﻿namespace Vdc.Grid
{
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    internal class GridGroupingArea : WebControl, INamingContainer
    {
        private Vdc.Grid.Grid grid;

        public GridGroupingArea(Vdc.Grid.Grid _grid)
        {
            this.grid = _grid;
            this.ID = this.GetGroupingAreaID();
        }

        protected override void CreateChildControls()
        {
            this.CssClass = this.grid.CssSettings.CSSGroupingAreaContainer + ((!this.grid.displayTopFooter && string.IsNullOrEmpty(this.grid.TemplateSettings.HeadingTemplateId)) ? "" : " ob_gGAContWT");
            HtmlContainer child = new HtmlContainer();
            HtmlContainer mainContainer = new HtmlContainer();
            HtmlContainer container3 = new HtmlContainer();
            child.CssClass = "ob_gGACnL";
            mainContainer.CssClass = this.grid.CssSettings.CSSGroupingArea;
            container3.CssClass = "ob_gGACnR";
            if (this.grid.IsDesignTimeRender && this.grid.UseEmbeddedImages)
            {
                child.Style["background-image"] = "url(" + this.grid.ImagesURL.Grouping_GroupingAreaLeft + ")";
                mainContainer.Style["background-image"] = "url(" + this.grid.ImagesURL.Grouping_GroupingArea + ")";
                container3.Style["background-image"] = "url(" + this.grid.ImagesURL.Grouping_GroupingAreaRight + ")";
            }
            child.Text = "&#160;";
            container3.Text = "&#160;";
            if (this.grid.GroupBy == string.Empty)
            {
                HtmlContainer container4 = new HtmlContainer {
                    CssClass = this.grid.CssSettings.CSSGroupingAreaDefaultColumn,
                    Text = this.grid.LocalizationSettings.Grouping_GroupingAreaText
                };
                mainContainer.Controls.Add(container4);
            }
            else
            {
                this.CreateGroupingAreaColumns(mainContainer);
            }
            this.Controls.Add(child);
            this.Controls.Add(mainContainer);
            this.Controls.Add(container3);
        }

        protected virtual void CreateGroupingAreaColumns(HtmlContainer mainContainer)
        {
            string[] strArray = this.grid.GroupBy.Split(new char[] { ',' });
            for (int i = 0; i < strArray.Length; i++)
            {
                Column columnByDataField = this.grid.Columns.GetColumnByDataField(strArray[i]);
                if ((columnByDataField != null) && columnByDataField.AllowGroupBy)
                {
                    HtmlContainer child = new HtmlContainer();
                    HtmlContainer container2 = new HtmlContainer();
                    HtmlContainer container3 = new HtmlContainer();
                    HtmlContainer container4 = new HtmlContainer();
                    HtmlContainer container5 = new HtmlContainer();
                    child.CssClass = this.grid.CssSettings.CSSGroupingAreaColumn;
                    container2.CssClass = this.grid.CssSettings.CSSGroupingAreaColumnLeft;
                    container3.CssClass = this.grid.CssSettings.CSSGroupingAreaColumnContent;
                    container4.CssClass = this.grid.CssSettings.CSSData;
                    container5.CssClass = this.grid.CssSettings.CSSGroupingAreaColumnRight;
                    container2.Text = "&#160;";
                    container3.Text = columnByDataField.HeaderText;
                    container4.Text = columnByDataField.DataField;
                    container5.Text = "&#160;";
                    if (i > 0)
                    {
                        HtmlContainer container6 = new HtmlContainer {
                            CssClass = this.grid.CssSettings.CSSGroupingAreaColumnSpacer
                        };
                        if (this.grid.IsDesignTimeRender && this.grid.UseEmbeddedImages)
                        {
                            container6.Style["background-image"] = "url(" + this.grid.ImagesURL.Grouping_GroupingAreaColumnSpacer + ")";
                        }
                        mainContainer.Controls.Add(container6);
                    }
                    if (this.grid.IsDesignTimeRender && this.grid.UseEmbeddedImages)
                    {
                        container2.Style["background-image"] = "url(" + this.grid.ImagesURL.Grouping_GroupingAreaColumnLeft + ")";
                        container3.Style["background-image"] = "url(" + this.grid.ImagesURL.Grouping_GroupingAreaColumn + ")";
                        container5.Style["background-image"] = "url(" + this.grid.ImagesURL.Grouping_GroupingAreaColumnRight + ")";
                    }
                    child.Controls.Add(container2);
                    child.Controls.Add(container3);
                    if (!this.grid.IsDesignTimeRender)
                    {
                        child.Controls.Add(container4);
                    }
                    child.Controls.Add(container5);
                    mainContainer.Controls.Add(child);
                }
            }
        }

        internal void ForceEnsureChildControls()
        {
            this.EnsureChildControls();
        }

        protected virtual string GetGroupingAreaID()
        {
            return ("ob_" + this.grid.GridID + "GroupingArea");
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }
    }
}

